package com.shd.edanpei.base.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.MyApplication;
import com.shd.edanpei.ui.activity.NewCustomerActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.view.addressPicker.MultipleAddressPicker;
import com.shd.edanpei.view.picker.AddressPickerView;
import com.shd.edanpei.view.picker.DatePicker;
import com.shd.edanpei.view.picker.DoubleDataPicker;
import com.shd.edanpei.view.picker.List4ItemPicker;
import com.shd.edanpei.view.picker.ListPicker;
import com.shd.edanpei.view.picker.ListUnitPicker;
import com.shd.edanpei.view.picker.RatePicker2;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by mc on 2018/9/3.
 * yang-wen-wen
 */

public class DialogUtil {


    /**
     * 联系客服
     */
    public static void callService(final Context context) {
        final String companyServerPhone = DaoManager.getInstance().getUserBean().getCompanyServerPhone();
        DialogUtil.infoDialog(context, "联系客服", "客服电话：" + companyServerPhone + "\n(工作日：9:00-22:00)", null, "拨号",
                new DialogUtil.SureListener() {
                    @Override
                    public void sure() {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + companyServerPhone));
                        context.startActivity(intent);
                    }
                });
    }


    /**
     * 拨号
     */
    public static void call(final Context context, final String number) {
        DialogUtil.infoDialog(context, number, null, "取消", "拨号",
                new DialogUtil.SureListener() {
                    @Override
                    public void sure() {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number));
                        context.startActivity(intent);
                    }
                });
    }


    //列表选择器
    public static void listDialog(Context context, String title, int arrayId, final Listener listener) {
        String[] strings = context.getResources().getStringArray(arrayId);
        ListPicker picker = new ListPicker(context, title, Arrays.asList(strings), new ListPicker.ResultHandler() {
            @Override
            public void handle(String data) {
                listener.done(data);
            }
        });
        picker.setIsLoop(false);
        picker.show();
    }

    //日期选择器
    public static void dateDialog(Context context, String title, final Listener listener) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String now = sdf.format(new Date());
        DatePicker customDatePicker = new DatePicker(context, title, new DatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                listener.done(time);
            }
        }, "1900-01-01 00:00", "2100-01-01 00:00");
        customDatePicker.showSpecificTime(false);
        customDatePicker.setIsLoop(false);
        customDatePicker.show(now);
    }

    //金额选择
    public static void moneyLimitDialog(Context context, String title, String unit, final Listener listener) {
        List4ItemPicker picker = new List4ItemPicker(context, title, unit, new List4ItemPicker.ResultHandler() {
            @Override
            public void handle(String str) {
                if (Integer.parseInt(str) > 0) {
                    listener.done(Integer.parseInt(str) + "");
                }

            }
        });
        picker.show();
    }

    //预期比
    public static void rateListDialog(Context context, final RateListener listener) {
        ListUnitPicker picker = new ListUnitPicker(context, "预期比",
                Constant.Rate.MIN.getRate(),
                Constant.Rate.MAX.getRate(),
                Constant.Rate.GAINS.getRate(),
                Constant.Unit.LI.getUnit(),
                new ListUnitPicker.ResultHandler() {
                    @Override
                    public void handle(String str) {
                        listener.done(str + Constant.Unit.LI.getUnit(), Constant.Rate.MIN.getRate() + "", str);
                    }
                });
        picker.show();
    }

    /**
     * 带单位列表选择器
     *
     * @param context
     */
    public static void showRatePicker2(Context context, String title, final Listener listener, double min,
                                       double max, double gains, String unit) {
        RatePicker2 ratePicker = new RatePicker2(context, new RatePicker2.ResultHandler() {
            @Override
            public void handle(String time) {
                listener.done(time);
            }
        }, min, max, gains, unit, title);
        ratePicker.show();
    }

    //年龄选择
    public static void ageListDialog(Context context, final RateListener listener) {
        DoubleDataPicker picker = new DoubleDataPicker(context, "年龄要求",
                Constant.Age.MIN.getAge(),
                Constant.Age.MAX.getAge(),
                Constant.Age.GAINS.getAge(),
                Constant.Unit.AGE.getUnit(),
                new DoubleDataPicker.ResultHandler() {
                    @Override
                    public void handle(String str) {
                        listener.done(Constant.Age.MIN.getAge() + "—" + str + Constant.Unit.AGE.getUnit(), Constant.Age.MIN.getAge() + "", str);
                    }
                });
        picker.show();
    }


    public static void infoDialog(Context context, String title, String content, String cancel,
                                  String sure, final SureListener listener) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_a, null);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvContent = view.findViewById(R.id.tv_content);
        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        TextView tvSure = view.findViewById(R.id.tv_sure);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setText(title);
        }
        if (TextUtils.isEmpty(content)) {
            tvContent.setVisibility(View.GONE);
        } else {
            tvContent.setText(content);
        }
        if (TextUtils.isEmpty(cancel)) {
            tvCancel.setText("取消");
        } else {
            tvCancel.setText(cancel);
        }
        if (TextUtils.isEmpty(sure)) {
            tvSure.setText("确定");
        } else {
            tvSure.setText(sure);
        }

        final Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }

        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (null != listener) {
                    listener.sure();
                }
            }
        });
        dialog.show();
    }

    /**
     * 身份确认
     *
     * @param context
     * @param listener
     */
    public static void sureRole(Context context, String roleName, final SureListener listener) {
        final View view = LayoutInflater.from(context).inflate(R.layout.dialog_sure_role, null);
        final TextView tvRoleName = view.findViewById(R.id.tv_roleName);
        final TextView tvCancel = view.findViewById(R.id.tv_cancel);
        final TextView tvSure = view.findViewById(R.id.tv_sure);

        tvRoleName.setText(roleName);

        final Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }

        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (null != listener) {
                    listener.sure();
                }
            }
        });
        dialog.show();
    }


    public static void infoDialog2(Context context, String title, String content,
                                   String sure, final SureListener listener) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_b, null);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvContent = view.findViewById(R.id.tv_content);
        TextView tvSure = view.findViewById(R.id.tv_sure);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setText(title);
        }
        if (TextUtils.isEmpty(content)) {
            tvContent.setVisibility(View.GONE);
        } else {
            tvContent.setText(content);
        }
        if (TextUtils.isEmpty(sure)) {
            tvSure.setText("确定");
        } else {
            tvSure.setText(sure);
        }

        final Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.sure();
                }
                dialog.dismiss();
            }
        });
        dialog.show();
    }


    public static void showNewDialog(final Context context) {
        final Dialog dialog = new Dialog(context, R.style.BottomDialog);
        final View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_new, null);
        View viewA = contentView.findViewById(R.id.tv_a);
        View viewB = contentView.findViewById(R.id.tv_b);
        View cancel = contentView.findViewById(R.id.tv_cancel);

        viewA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(context, NewOrderActivity.class);
//                intent.putExtra(Constant.ID, HomeFragment.TAB_ID);
//                context.startActivity(intent);
//                dialog.dismiss();
            }
        });
        viewB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, NewCustomerActivity.class));
                dialog.dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        dialog.setContentView(contentView);
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        layoutParams.width = contentView.getResources().getDisplayMetrics().widthPixels;
        contentView.setLayoutParams(layoutParams);
        dialog.getWindow().setGravity(Gravity.BOTTOM);
        dialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        dialog.show();
    }


    /**
     * 省市区选择器
     */
    public static void addressPick(Context context, final AddressListener listener) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.picker_address_pop, null);
        AddressPickerView addressView = rootView.findViewById(R.id.apvAddress);

        final Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(rootView);
        addressView.setOnAddressPickerSure(new AddressPickerView.OnAddressPickerSureListener() {
            @Override
            public void onSureClick(String address, String provinceCode, String cityCode, String districtCode) {
                listener.done(address, provinceCode, cityCode, districtCode);
                dialog.dismiss();
            }
        });
        dialog.setCanceledOnTouchOutside(true);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        dialog.show();
    }

    public static Dialog multipleAddressPicker(Context context, final MultipleListener listener) {
        View view = LayoutInflater.from(context).inflate(R.layout.picker_address_mulitple, null);
        MultipleAddressPicker picker = view.findViewById(R.id.multiple_address);

        final Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        picker.setListener(new MultipleAddressPicker.Listener() {
            @Override
            public void cancel() {
                dialog.dismiss();
            }

            @Override
            public void sure(String desc, List<String> code) {
                listener.sure(desc, code);
                dialog.dismiss();

            }
        });
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        return dialog;
    }

    public interface MultipleListener {

        void sure(String desc, List<String> code);
    }

    /**
     * 成功Dialog
     *
     * @param context
     * @param listener
     */
    public static void showSuccessDialog(Context context, final SureListener listener, String... str) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_success, null);
        TextView tvContent = view.findViewById(R.id.tv_dialog_apply_success_content);
        TextView tvHint = view.findViewById(R.id.tv_dialog_apply_success_hint);
        TextView tvSure = view.findViewById(R.id.tv_dialog_apply_success_sure);
        tvContent.setText(str[0]);
        tvHint.setText(str[1]);
        final Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.sure();
                dialog.dismiss();
            }

        });
        dialog.show();
    }

    /**
     * 抢单成功dialog
     */
    public static void showTakeSuccessDialog(Context context, final TwoListener listener) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_take_success, null);
        TextView tvLeft = view.findViewById(R.id.tv_dialog_left);
        TextView tvRight = view.findViewById(R.id.tv_dialog_right);
        final Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.cancel();
                dialog.dismiss();
            }
        });
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.sure();
                dialog.dismiss();
            }

        });
        dialog.show();
    }


    /**
     * Dialog 4
     *
     * @param context
     * @param strings
     */
    public static void showDialog4(Context context, final DialogUtil.SureListener listListener, String... strings) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_d, null);
        TextView tvTitle = view.findViewById(R.id.tv_dialog_a_title);
        TextView tvContent = view.findViewById(R.id.tv_dialog_a_content);
        TextView tvSure = view.findViewById(R.id.tv_dialog_a_sure);
        tvTitle.setText(strings[0]);
        tvContent.setText(strings[1]);
        tvSure.setText(strings[2]);
        final Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listListener != null) {
                    listListener.sure();
                }
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public static void hintDialog(Context context, @DrawableRes int logo, String hint) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_hint, null);
        ImageView imageView = view.findViewById(R.id.imgView);
        TextView textView = view.findViewById(R.id.hint);
        imageView.setImageResource(logo);
        textView.setText(hint);
        final Dialog dialog = new Dialog(context, R.style.dialog);

        dialog.setContentView(view);
        dialog.getWindow().setDimAmount(0);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        };
        new Timer().schedule(timerTask, 2000);

    }


    public interface AddressListener {

        void done(String address, String provinceCode, String cityCode, String districtCode);
    }

    public interface Listener {

        void done(String data);
    }

    public interface RateListener {

        void done(String str, String min, String max);
    }

    public interface SureListener {

        void sure();
    }


    public interface TwoListener {

        void cancel();

        void sure();
    }
}
